package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        return roleService.findPage(queryPageBean);
    }


    @RequestMapping("/findAllPermissionAndMenu")
    public Result findAllPermission() {
        try {
            List<Permission> permissionList = roleService.findAllPermission();
            return new Result(true, MessageConstant.GET_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_FAIL);
        }
    }

    @RequestMapping("/addRole")
    public Result addRole(Integer[] permissionsIds,@RequestBody Role role ){
        try {
            roleService.addRole(permissionsIds, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
        return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
    }


    @RequestMapping("/findOneRole")
    public Result findOneRole(Integer roleId) {
        try {
            Role role = roleService.findOneRole(roleId);
            return new Result(true, MessageConstant.GET_ROLE_LIST_SUCCESS, role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ROLE_LIST_FAIL);
        }
    }

    @RequestMapping("/findAllPermissionByRoleId")
    public Result findAllPermissionByRoleId(Integer roleId) {
        try {
            List<Integer> permissionList = roleService.findAllPermissionByRoleId(roleId);
            return new Result(true, MessageConstant.GET_PERMISSION_SUCCESS, permissionList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_PERMISSION_FAIL);
        }
    }


    @RequestMapping("/editRole")
    public Result editRole(Integer[] permissionsIds, @RequestBody Role role) {
        try {
            roleService.editRole(permissionsIds, role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }

    @RequestMapping("/deleteRoleById")
    public Result deleteRoleById(Integer roleId) {
        try {
            roleService.deleteRoleById(roleId);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
